package cn.gzjp.modules.it.api.web;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.modules.it.api.bean.CommonJson;
import cn.gzjp.modules.it.entity.Car;
import cn.gzjp.modules.it.entity.Member;
import cn.gzjp.modules.it.service.CarService;
import cn.gzjp.modules.it.service.MemberCarService;
import cn.gzjp.modules.it.service.MemberService;
import cn.gzjp.modules.it.entity.MemberCar;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 车辆违章管理
 */
@Controller("frontCar")
@Api(value = "车辆管理", description = "车辆管理")
@RequestMapping(value = "${frontPath}/car")
public class CarController extends BaseController {


	@Autowired
	private CarService carService;
	@Autowired
	private MemberCarService memberCarService;
	@Autowired
	private MemberService memberService;
	
	 /**
     * 获取车辆
     */
	@ApiOperation(value = "车辆列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
	@ApiImplicitParams ({@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
      })
	@ResponseBody
    public String cars(String openid) {
		log.info("车辆列表查询 openid ="+openid);
		CommonJson result = new CommonJson();
    	if (StringUtils.isBlank(openid)) {
    		result.setMsg("openid不能为空");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
        }
    	if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
        try {
        	Member member = memberService.get(openid);
        	if(member!=null){
        		result.setData(member.getCarList());
        	}else{
        		result.setCode(-1);//不存在
        		result.setMsg("用户不存在");//不存在
        	}
        	log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        } catch (Exception e) {
        	result.setCode(1);//不存在
    		result.setMsg("查询错误");//不存在
            this.logger.error(e.getMessage(), e);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
    }
	
	@ApiOperation(value = "车辆新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="chepai",value="车牌",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="engineNO",value="发动机号",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="carVIN",value="车架号",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="mobile",value="手机号",dataType="String",paramType="form",required =true)
      })
	@ResponseBody
	public String addCar(String chepai, String engineNO, String carVIN,String openid,String mobile) {
		CommonJson result = new CommonJson();
		log.info("开始添加车辆信息" + chepai);
		log.info("车牌 chepai=" + chepai);
		log.info("发动机号 engineNO=" + engineNO);
		log.info("车架号 carVIN=" + carVIN);
		log.info("openid=" + openid);
		log.info("mobile=" + mobile);
		if (StringUtils.isBlank(chepai) || StringUtils.isBlank(engineNO) || StringUtils.isBlank(carVIN)|| StringUtils.isBlank(openid)) {
			result.setMsg("参数不能为空");
			result.setCode(1);
			return this.mapper.toJson(result);
		}
		if (chepai.length() != 7 || engineNO.length() < 6 || carVIN.length() < 6) {
			result.setMsg("参数长度不正确");
			result.setCode(2);
			return this.mapper.toJson(result);
		}
		if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
		Car car = carService.get(chepai);
		if(car==null){
			car = new Car();
			car.setId(chepai);
			car.setMobile(mobile);
			car.setCarVIN(carVIN);
			car.setEngineNO(engineNO);
			car.setMobile(mobile);
		}
		carService.save(car,openid);
		return this.mapper.toJson(result);
	}
	
	@ApiOperation(value = "车辆修改")
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="chepai",value="车牌",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="engineNO",value="发动机号",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="carVIN",value="车架号",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="mobile",value="手机号",dataType="String",paramType="form",required =true)
      })
	@ResponseBody
	public String editCar(String chepai, String engineNO, String carVIN,String openid,String mobile) {
		CommonJson result = new CommonJson();
		log.info("开始修改车辆信息" + chepai);
		log.info("车牌 chepai=" + chepai);
		log.info("发动机号 engineNO=" + engineNO);
		log.info("车架号 carVIN=" + carVIN);
		log.info("openid=" + openid);
		log.info("mobile=" + mobile);
		if (StringUtils.isBlank(chepai) || StringUtils.isBlank(engineNO) || StringUtils.isBlank(carVIN)|| StringUtils.isBlank(openid)) {
			result.setMsg("参数不能为空");
			result.setCode(1);
			return this.mapper.toJson(result);
		}
		if (chepai.length() != 7 || engineNO.length() < 6 || carVIN.length() < 6) {
			result.setMsg("参数长度不正确");
			result.setCode(2);
			return this.mapper.toJson(result);
		}
		Car car = carService.get(chepai);
		if(car!=null){
			car.setMobile(mobile);
			car.setCarVIN(carVIN);
			car.setEngineNO(engineNO);
			car.setMobile(mobile);
		}else{
			car = new Car();
			car.setId(chepai);
			car.setMobile(mobile);
			car.setCarVIN(carVIN);
			car.setEngineNO(engineNO);
			car.setMobile(mobile);
		}
		carService.save(car,openid);
		return this.mapper.toJson(result);
	}
	
	@ApiOperation(value = "车辆置顶")
	@RequestMapping(value = "setTop", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="chepai",value="车牌",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true)
      })
	@ResponseBody
	public String setTop(String chepai,String openid) {
		CommonJson result = new CommonJson();
		log.info("车辆置顶" + chepai);
		log.info("车牌 chepai=" + chepai);
		log.info("openid=" + openid);
		if (StringUtils.isBlank(chepai)|| StringUtils.isBlank(openid)) {
			result.setMsg("参数不能为空");
			result.setCode(1);
			return this.mapper.toJson(result);
		}
		if (chepai.length() != 7 || openid.length() < 6 ) {
			result.setMsg("参数长度不正确");
			result.setCode(2);
			return this.mapper.toJson(result);
		}
		if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
		MemberCar mc = new MemberCar();
		mc.setCarId(chepai);
		mc.setMemberId(openid);
		Integer maxSort = memberCarService.findMaxSort(mc);
		MemberCar memberCar = memberCarService.findByMemberIdAndCarId(mc);
		memberCar.setSort(maxSort+10);
		memberCarService.updateSort(memberCar);
		return this.mapper.toJson(result);
	}
	
	@ApiOperation(value = "车辆删除")
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="chepai",value="车牌",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true)
      })
	@ResponseBody
	public String delete(String chepai,String openid) {
		CommonJson result = new CommonJson();
		log.info("删除车辆" + chepai);
		log.info("车牌 chepai=" + chepai);
		log.info("openid=" + openid);
		if (StringUtils.isBlank(chepai)|| StringUtils.isBlank(openid)) {
			result.setMsg("参数不能为空");
			result.setCode(1);
			return this.mapper.toJson(result);
		}
		if (chepai.length() != 7 || openid.length() < 6 ) {
			result.setMsg("参数长度不正确");
			result.setCode(2);
			return this.mapper.toJson(result);
		}
		MemberCar mc = new MemberCar();
		mc.setCarId(chepai);
		mc.setMemberId(openid);
		memberCarService.delete(mc);
		return this.mapper.toJson(result);
	}
	
	
}
